package com.dialog.ringsdk.demo;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dialog.ringsdk.R;

import java.util.List;

public class MyBluetoothDeviceAdapter extends RecyclerView.Adapter<MyBluetoothDeviceAdapter.ViewHolder> {
  List<BluetoothDevice> scannedDevices;
  Context context;
    private onItemClickListener onItemClickLintener;


    public MyBluetoothDeviceAdapter(List scannedDevices, Context context) {
        this.scannedDevices = scannedDevices;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_demo,null);
        return new ViewHolder(view);
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            holder.tv_name.setText(scannedDevices.get(position).getName()+"");
            holder.tv_adss.setText(scannedDevices.get(position).getAddress()+"");
            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickLintener.onItemClick(scannedDevices.get(position));
                }
            });
    }

    @Override
    public int getItemCount() {
        return scannedDevices.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_adss;
        LinearLayout ll_item;

        ViewHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_adss = itemView.findViewById(R.id.tv_adss);
            ll_item = itemView.findViewById(R.id.ll_item);
        }
    }

    public void setOnItemClickListener( onItemClickListener onItemClickLintener){
        this.onItemClickLintener = onItemClickLintener;
    }
    interface  onItemClickListener{
       void onItemClick(BluetoothDevice bluetoothDevice);
    }
}
